// Created by WXX on 2021/10/23 15:41
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int minDistance(string a, string b) {

        int n = a.size(), m = b.size();
        a = ' ' + a, b = ' ' + b;

        vector<vector<int>> f(n + 1, vector<int>(m + 1));
        // 初始化
        for (int j = 0; j <= m; j++) f[0][j] = j;  // 将空字符串变为b[1~j]需要j步添加操作
        for (int i = 0; i <= n; i++) f[i][0] = i;  // 将a[1~i]变为空字符串需要i步删除操作

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++) {
                f[i][j] = min(f[i - 1][j] + 1, f[i][j - 1] + 1);
                if (a[i] == b[j]) f[i][j] = min(f[i][j], f[i - 1][j - 1]);
                else f[i][j] = min(f[i][j], f[i - 1][j - 1] + 1);
            }
        return f[n][m];
    }
};

int main() {

    cout << Solution().minDistance("horse", "ros") << endl;  // 3

    return 0;
}
